package com.epam.xml.export;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.log4j.Logger;

import com.epam.xml.bank.Account;
import com.epam.xml.bank.Card;
import com.epam.xml.bank.Client;
import com.epam.xml.bank.Clients;

public class JaxbMarshal {
	private static Logger logger=Logger.getLogger(JaxbMarshal.class);
	private static Random rand=new Random();
	
	public static void main(String[] args) {
		try {
			JAXBContext context = JAXBContext.newInstance(Clients.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			List<Client> clientList = new ArrayList<Client>();
			for (int i=1; i<=4; i++) {
				Client client = new Client();
				client.setId(i);
				client.setLogin("login"+i);
				client.setPassword("password"+i);
				client.setName("name"+i);
				int numOfAccounts=0;
				if (i%2==0) {
					numOfAccounts=2;
				} else {
					numOfAccounts=1;
				}
				for (int j=0; j<numOfAccounts;j++) {
					HashMap<String, Integer> currencyAndAmount = new HashMap<String, Integer>();
					currencyAndAmount.put(randomCurrency(), rand.nextInt(500000));
					Account account = new Account(i+j+1, currencyAndAmount, new Card(randomCardType()));
					client.addAccount(account);
				}
				clientList.add(client);
			}
			Clients clients=new Clients();
			clients.setClients(clientList);
			m.marshal(clients, new FileOutputStream("Jaxb.xml"));

		} catch (JAXBException e) {
			logger.error(e);
		} catch (FileNotFoundException e) {
			logger.error(e);
		}

	}
	
	private static String randomCurrency() {
		switch(rand.nextInt(3)) {
		case 0:
			return "EUR";
		case 1: 
			return "USD";
		case 2:
			return "KZT";
		}
		return null;
	}
	
	private static String randomCardType() {
		switch(rand.nextInt(7)) {
		case 0:
			return "VISA";
		case 1:
			return "AMERICAN_EXPRESS";
		case 2:
			return "MASTER_CARD";
		case 3:
			return "MAESTRO";
		case 4:
			return "CIRRUS";
		case 5:
			return "DINERS_CLUB";
		case 6:
			return "JCB";
		}
		return null;
	}


}
